package com.airlines.system.service.impl;

import java.util.List;

import com.airlines.system.dao.SysRoleMapper;
import com.airlines.system.domain.SysRole;
import com.airlines.system.domain.SysUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.airlines.base.constant.UserConstant;
import com.airlines.base.service.base.impl.BaseServiceImpl;
import com.airlines.system.dao.SysUserMapper;
import com.airlines.system.service.SysUserService;

@Service
public class SysUserServiceImpl extends BaseServiceImpl<SysUser, String> implements SysUserService {

	@Autowired
	private SysUserMapper sysUserMapper;
	@Autowired
	private SysRoleMapper sysRoleMapper;

	@Autowired
	public void setBaseMapper() {
		super.setBaseMapper(sysUserMapper);
	}

	@Override
	public SysUser getUserByWorkNo(String workNo) {
		SysUser sysUser = new SysUser();
		sysUser.setWorkNo(workNo);
		List<SysUser> list = sysUserMapper.selectByQuery(sysUser);
		if (null != list && list.size() == 1) {
			// 如果查询有且仅有一个用户，则返回该用户
			return list.get(0);
		}
		return null;
	}

	@Override
	public SysUser getUserByUserName(String userName) {
		SysUser queryUser = new SysUser();
		queryUser.setUserName(userName);
		List<SysUser> list = sysUserMapper.selectByQuery(queryUser);
		if (list != null && list.size() == 1) {
			// 如果查询有且仅有一个用户，则返回该用户
			return list.get(0);
		}
		return null;
	}

	@Override
	public boolean userUsable(SysUser sysUser) {
		boolean able = false;
		if (sysUser != null
				&& StringUtils.equals(sysUser.getStatus().toString(), UserConstant.USER_STATUS_USABLE.toString())) {
			able = true;
		}
		return able;
	}

	@Override
	public List<SysUser> findAllUser(String userNo, String userName) {
		SysUser sysUser = new SysUser();
		sysUser.setWorkNo(userNo);
		sysUser.setWorkNo(userName);
		return sysUserMapper.selectByQuery(sysUser);
	}

	@Override
	public SysUser findAllRole(String userId) {
		List<SysRole> roles = sysRoleMapper.findAllByType("1");
		SysUser user = sysUserMapper.selectByPrimaryKey(userId);
		user.setRoleList(roles);
		return user;
	}

	@Override
	public SysUser findOwnRole(String userId) {
		List<SysRole> roles = sysRoleMapper.findByTypeAndUser("1",userId);
		SysUser user = sysUserMapper.selectByPrimaryKey(userId);
		user.setRoleList(roles);
		return user;
	}

}
